//
//  WNBaseTabBarController.m
//  iOSOCProjectDirectory
//
//  Created by BHmac on 2017/6/8.
//  Copyright © 2017年 XieXuan. All rights reserved.
//

#import "WNBaseTabBarController.h"
// 导航栏控制器
#import "WNBaseNavigationController.h"
// 主界面控制器
#import "WNHomeViewController.h"
#import "WNHappyLifeViewController.h"
#import "WNSetUpViewController.h"
// 标签所需数据模型
#import "WNTabBarDataModel.h"

@interface WNBaseTabBarController ()

@property(strong , nonatomic)NSMutableArray *tabBarDataArr;

@end

@implementation WNBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置所有标签栏属性
    [self setupItemTitleTextAttrs];
    // 添加自控制器
    [self setupAddController];
    
}

- (void)setupItemTitleTextAttrs{
    
    // 设置normal状态X下文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    
    // 设置Selected状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    // 利用Appearance对象统一设置文字属性
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
}

- (void)setupAddController{
    
    for (int i = 0; i < self.tabBarDataArr.count; i++) {
        WNTabBarDataModel *mdoel = self.tabBarDataArr[i];
        [self setupOneChildVc:[[WNBaseNavigationController alloc] initWithRootViewController:mdoel.childVC] Title:mdoel.titleStr Image:mdoel.imageStr SelectImage:mdoel.selectImaagStr];
    }
    
}

- (void)setupOneChildVc:(UIViewController *)childVc Title:(NSString *)title Image:(NSString *)image SelectImage:(NSString *)selectImage{
    
    childVc.navigationItem.title = title;
    childVc.tabBarItem.title = title;
    if (image.length) {
        childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    if (selectImage.length) {
        childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    [self addChildViewController:childVc];
    
}

#pragma mark -------------------------------------------- 标签栏所需数据
- (NSMutableArray *)tabBarDataArr{
    
    if (!_tabBarDataArr) {
        _tabBarDataArr = [NSMutableArray array];
        
        [self.tabBarDataArr addObject:[WNTabBarDataModel setWNTabBarDataModelWithChildVc:[[WNHomeViewController alloc]init] Title:@"首页" Image:@"tab_home_unselected" SelectImage:@"tab_home_onselected"]];
        [self.tabBarDataArr addObject:[WNTabBarDataModel setWNTabBarDataModelWithChildVc:[[WNHappyLifeViewController alloc]init] Title:@"生活" Image:@"tab_work_order_unselected" SelectImage:@"tab_work_order_onselected"]];
        [self.tabBarDataArr addObject:[WNTabBarDataModel setWNTabBarDataModelWithChildVc:[[WNSetUpViewController alloc]init] Title:@"我的" Image:@"tab_my_unselected" SelectImage:@"tab_my_onselected"]];
        
    }
    return _tabBarDataArr;
    
}

@end
